package com.moon.game.util;

import org.apache.commons.lang3.StringUtils;

import com.moon.game.clazz.PBMessageProto.PBMessage;
import com.moon.game.command.RequestSession;
import com.moon.game.exception.GException;



/**
 * Proto 辅助工具类
 * 
 * 
 */
public class MessageUtil {

    /**
     * 构建消息对象
     * 
     * @param reqSession
     * @param cmd
     * @param targetId
     * @return
     * @throws GException
     */
    public static PBMessage.Builder buildMessage(final RequestSession reqSession, final int cmd, final String targetId) throws GException {
        if (StringUtils.isBlank(targetId)) {
            throw new GException("build message err,targetId is blank!");
        }

        return (reqSession != null) ? MessageUtil.buildReturnMessage(reqSession, cmd, targetId) : MessageUtil.buildNoReturnMessage(cmd, targetId);
    }

    /**
     * 构建消息对象
     * 
     * @param cmd
     * @param targetId
     * @return
     * @throws GException
     */
    public static PBMessage.Builder buildMessage(final int cmd, final String targetId) throws GException {
        return buildMessage(null, cmd, targetId);
    }

    /**
     * 构建有返回消息对象
     * 
     * @param cmd
     * @param req
     * @param pid
     * @return
     */
    private static PBMessage.Builder buildReturnMessage(final RequestSession reqSession, final int cmd, final String targetId) {
        PBMessage.Builder request = PBMessage.newBuilder();
        request.setId(targetId);
        request.setCmd(cmd);
        request.setStatus(reqSession.getStatusPlus());
        request.setSessionId(reqSession.getSessionId());
        return request;
    }

    /**
     * 构建无返回消息对象
     * 
     * @param cmd
     * @param pid
     * @return
     */
    private static PBMessage.Builder buildNoReturnMessage(final int cmd, final String targetId) {
        PBMessage.Builder request = PBMessage.newBuilder();
        request.setId(targetId);
        request.setCmd(cmd);
        request.setIsReturn(false);
        return request;
    }

}
